//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbPhongBan
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbPhongBan_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbPhongBan_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbPhongBan OTbPhongBan)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"TenPB", OTbPhongBan.TenPB);
            pr[1] = new SqlParameter(@"DiaChiPB", OTbPhongBan.DiaChiPB);
            pr[2] = new SqlParameter(@"TruongPB", OTbPhongBan.TruongPB);
            pr[3] = new SqlParameter(@"SoNV", OTbPhongBan.SoNV);
            pr[4] = new SqlParameter(@"TrangThai", OTbPhongBan.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbPhongBan_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbPhongBan OTbPhongBan)
        {
            SqlParameter[] pr = new SqlParameter[6];
            pr[0] = new SqlParameter(@"MaPB", OTbPhongBan.MaPB);
            pr[1] = new SqlParameter(@"TenPB", OTbPhongBan.TenPB);
            pr[2] = new SqlParameter(@"DiaChiPB", OTbPhongBan.DiaChiPB);
            pr[3] = new SqlParameter(@"TruongPB", OTbPhongBan.TruongPB);
            pr[4] = new SqlParameter(@"SoNV", OTbPhongBan.SoNV);
            pr[5] = new SqlParameter(@"TrangThai", OTbPhongBan.TrangThai);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbPhongBan_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaPB)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaPB",MaPB);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbPhongBan_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbPhongBan SelectByID(int MaPB)
        {
            ETbPhongBan OTbPhongBan = new ETbPhongBan();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaPB",MaPB);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbPhongBan_SelectByID", pr);
            if (idr.Read())
              OTbPhongBan = GetOneTbPhongBan(idr);
            idr.Close();
            idr.Dispose();
            return OTbPhongBan;
        }
//-----------------------------------------------------------//
        private static ETbPhongBan GetOneTbPhongBan(IDataReader idr)
       {
            ETbPhongBan OTbPhongBan = new ETbPhongBan();
                if (idr["MaPB"] != DBNull.Value)
                    OTbPhongBan.MaPB = (int)idr["MaPB"];
                if (idr["TenPB"] != DBNull.Value)
                    OTbPhongBan.TenPB = (string)idr["TenPB"];
                if (idr["DiaChiPB"] != DBNull.Value)
                    OTbPhongBan.DiaChiPB = (string)idr["DiaChiPB"];
                if (idr["TruongPB"] != DBNull.Value)
                    OTbPhongBan.TruongPB = (string)idr["TruongPB"];
                if (idr["SoNV"] != DBNull.Value)
                    OTbPhongBan.SoNV = (int)idr["SoNV"];
                if (idr["TrangThai"] != DBNull.Value)
                    OTbPhongBan.TrangThai = (int)idr["TrangThai"];
            return OTbPhongBan;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaPB)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaPB",MaPB);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbPhongBan_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbPhongBan> ListAll()
        {
            List<ETbPhongBan> list = new List<ETbPhongBan>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbPhongBan_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbPhongBan(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbPhongBan> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbPhongBan> list = new List<ETbPhongBan>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbPhongBan_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbPhongBan(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}